package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Client;

import dao.DAOClient;
import dao.DAOFactory;

/**
 * Servlet implementation class SeConnecter
 */
@WebServlet("/SeConnecter")
public class SeConnecter extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeConnecter() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nom = request.getParameter("nom");
		String mdp = request.getParameter("mdp");
		
		DAOClient dao = DAOFactory.getDAOClient();  
		
		//Recuperation du mot de passe et comparaison
		Client c = dao.find(nom);
		if( c!=null && c.getMdp().equals(mdp)){
			request.getSession().setAttribute("idUser", c.getId());
			request.getSession().setAttribute("nomUser", c.getNom());
			response.sendRedirect("/Derpizza/");
		}else{
			response.sendRedirect("/Derpizza/");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
